<!DOCTYPE html>
<html>

	<head lang="en">
		<title>骡窝日报</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/news.css" />
		<link rel="stylesheet" href="/js/plugins/dialog/dialog.css" />
		<script src="/js/plugins/jrender/jrender.js"></script>
		<link rel="stylesheet" href="/js/plugins/jquery-confirm/css/jquery-confirm.css">
		<script src="/js/plugins/jquery-confirm/js/jquery-confirm.js"></script>
		<script src="/js/plugins/dialog/dialog.min.js"></script>
		<script src="/js/common.js"></script>
	
	</head>

	<body>
		

		<div class="search-head">
			<div class="row nav-search">
				<div class="col-2">
					<a href="index.html">
						<span><i class="fa fa-chevron-left fa-2x"></i></span>
					</a>
				</div>
				<div class="col-10">
					<div class="input-group-sm search">
						<input class="form-control searchBtn" placeholder="搜索资讯">
					</div>
				</div>
			</div>
		</div>

		<div class="container news">
		
			<div class="news">
				<h6 class="news-title">骡窝日报 </h6>
				<div class="row" render-loop="list">
					<div class="col-6">
						<a href="/newsContent.html" data-id="id" render-key="list.id" render-fun="handle">
							<img class="rounded" width="100%" height="110px"  render-src="list.coverUrl">
						</a>
						<div class="caption">
							<p>
								<span > </span>
								<i class="fa fa-heart-o collectionIcon" data-id="" render-key="list.id" render-fun="collection">
								</i>
							</p>
							<a href="/newsContent.html" render-key="list.id" render-fun="handle">
								<h5 render-html="list.title"></h5>
								<p render-html="list.subTitle"></p>
							</a>
						</div>
					</div>

				</div>
			</div>
		</div>
<script>

	var currentPage = 1;
	var pages;
	var news;
    var user = JSON.parse(sessionStorage.getItem("user"));

    $(function () {
        //查询日报信息
        $.get("/news", {state: 1}, function (data) {
            //渲染日报
            $(".news").renderValues(data, {
                handle: function (ele, value) {
                    ele.href = "/newsContent.html?id="+value;
                }
            });
        });

        //发送ajax请求,获取日报
        $.get("/news", {state:1}, function (data) {
            //数据渲染
            renderViews(data);
            pages = data.pages;
            news = data.list;
        });

        //设置窗口的滚到事件
        $(window).scroll(function () {
            if (isEnd()) {
                if (currentPage < pages) {
                    currentPage++;
                    var args = {state: 1,currentPage: currentPage};
                    $.get("/news", args, function (data) {
                        //数据渲染
                        $.merge(news, data.list);
                        renderViews({list: news});
                    });
                } else {
                    $(document).dialog({
                        type : "notice",
                        infoText: "已经到底部了",
                        autoClose: 1500,
                        position: "center"
                    });
                }
            }
        });

        function renderViews(data) {
            $(".news").renderValues(data, {
                handle: function (ele, value) {
                    $(ele).attr("href", "/newsContent.html?id=" + value);
                },
                collection: function (ele, value) {
                    if (user) {
                        var collectionState;
                        ele.dataset.id = value;
                        //     //先判断当前
                        var userId = user.id;
                        var newsId = value;
                        $.ajaxSettings.async = false;
                        $.get("/users/" + userId + "/news/" + newsId, function (data) {
                            var a = data.data;
                            collectionState = a;
                        })
                        if (collectionState == "TRUE") {
                            $(ele).replaceWith('<i style="color:red" class="fa fa-heart collectionIcon" data-id="' + newsId + '"/>');
                        }
                    }
                }
            });
        }

        $(".news").on("click", ".collectionIcon", function () {
            var newsId = $(this).data("id");
            if (user) {
                var collectionState;
                var userId = user.id;
                console.log("开始");
                if($(this).is(".fa-heart-o")){
                    collectionState= "FALSE";
                }else {
                    collectionState ="TRUE";
                }
                console.log("完了之后"+collectionState);
                if (collectionState == "FALSE") {
                    //发送ajax请求,找到关联日报 增加
                    $.ajax({
                        url: "/users/" + userId + "/news",
                        type: "PUT",
                        data: {"newsId": newsId}
                    });
                    $(this).replaceWith('<i style="color:red" class="fa fa-heart collectionIcon" data-id="' + newsId + '"/>');
                    collectionState = "TRUE";
                    var data = $(this).data("id");
                } else if (collectionState == "TRUE") {
                    $.ajax({
                        url: "/users/" + userId + "/news/" + newsId,
                        type: "DELETE"
                    });
                    collectionState = "FALSE";
                    $(this).replaceWith('<i class="fa fa-heart-o collectionIcon" data-id="' + newsId + '"/>');
                }
            } else {
                $(document).dialog({
                    type: 'confirm',
                    closeBtnShow: true,
                    autoClose: 6000,
                    titleText: '温馨提示',
                    content: '请先登录',
                    buttonTextConfirm: '跳转到登录界面',
                    buttonTextCancel: '继续浏览',
                    onClickConfirmBtn: function () {
                        window.location.href = "/login.html";
                    }, onClickCancelBtn: function () {
                        window.location.reload();
                    }

                });
            }
        })

        //查询功能
		$(".searchBtn").blur(function () {
            var keyword = $(".searchBtn").val();
            if (keyword){
              $.get("/news",{keyword:keyword},function (data) {
				   sessionStorage.setItem("data",JSON.stringify(data));
                   location.href="/newsComment.html";
                });
            }
           if (!keyword)  {
               location.href="/newsPage.html";
		   }
        })

    })
</script>
</body>
</html>